<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/15
 * Time: 10:32
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Auth;
use Illuminate\Support\Facades\View;
use Session;
use Request;
use App\Services\ValidateCode;
use Redirect;
use \App\Services\Upload;
class IndexController extends Controller
{
    public function index(){
        if(!Session::get("user_info")){
            $url = url('/Log');
            return Redirect::to($url);
        }
        $blog_m = Model("Blog");
        $blogInfo = $blog_m->lastBlogInfo();
        $data = [];
        $data['nav'] = 'index';
        $data['lastBlogInfo'] = $blogInfo;
        return view('admin.index.index',$data);
    }

    public function other(){
        if(!Session::get("user_info")){
            $url = url('/Log');
            return Redirect::to($url);
        }
        $data = [];
        $data['nav'] = 'other';
        $list = Model("category")->getCates();
        $blog_m = Model("Blog");
        $blogList = $blog_m->getBlogListByUser(12);
        $data['list'] = $list;
        $data['blogList'] = $blogList;
        return view('admin.index.other',$data);

    }

    public function addCate(){
        if(!Session::get("user_info")){
            $url = url('/Log');
            return Redirect::to($url);
        }
        $data['name'] = trim(Request::input("name"));
        $data['id'] = trim(Request::input("id"));
        if(Request::input("img_id")){
            $data['img_id'] = trim(Request::input("img_id"));
        }

        $data['order'] = 0;

        if($data['id'] == 0){
            unset($data['id']);
        }
        $up = Upload::input2Res("img","upload/cate");

        if(!isset($data['id']) && !$up){
            $this->showMsg(0,"请上传一张图片","/Admin/Index/other");
        }
        if($up){
            $data['img_id'] = $up->id;
        }

        $cate_m = Model('Category');

        if(isset($data['id'])){
            $isExisted = $cate_m->where('name', $data['name'])->where("id","!=",$data['id'])->first();
        }else{
            $isExisted = $cate_m->where(['name' => $data['name']])->first();
        }
        if($isExisted){

            return $this->showMsg(0,"同名分类已经存在","/Admin/Index/other");
        }
        if(isset($data['id'])){
            $cate_m->where("id",$data['id'])->update($data);
        }else{
            $cate_m->fill($data)->save();

        }

        if(!$cate_m){
            return $this->showMsg(-1000,'',"/Admin/Index/other");
        }

        return $this->showMsg(2002,'',"/Admin/Index/other");
    }

    public function dropCate(){

        if(!Session::get("user_info")){
            $url = url('/Log');
            return Redirect::to($url);
        }
        $id = trim(Request::input("id"));
        if(!$id){
            return $this->send(-2001);
        }

        $cate_m = Model('Category');

        DB::beginTransaction();

        $info = $cate_m->find($id);

        // if($info){
        //     $rs0 = Model("Resource")->where("id","=",$info->img_id)->delete();
        // }

        $rs1 = $cate_m->where("id","=",$id)->delete();
        // DB::rollback();
        DB::commit();

        if($rs1){
            $code = 2001;
        }else{
            $code = -2003;
        }

        return $this->send($code);
    }
}